package com.nidu.demo.post.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.PageQuery;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nidu.demo.post.dto.PositionPageQry;
import com.nidu.demo.post.convertor.PositionConvertor;
import com.nidu.demo.post.gateway.PositionGateway;
import com.nidu.demo.post.mapper.PositionMapper;
import com.nidu.demo.post.mapper.dataobject.PositionDO;
import com.nidu.demo.post.model.Position;
import com.nidu.demo.post.model.PositionCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Set;
import java.util.List;

/**
 * 职位网关实现
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class PositionGatewayImpl implements PositionGateway {
    
    private final PositionMapper mapper;
    private final PositionConvertor convertor;

    @Override
    public Position create(Position post) {
        PositionDO postDO = convertor.toDataObject(post);
        mapper.insert(postDO);
        return convertor.toDomainObject(postDO);
    }

    @Override
    public Position update(Position post) {
        PositionDO postDO = convertor.toDataObject(post);
        mapper.updateById(postDO);
        return convertor.toDomainObject(postDO);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Set<Long> ids) {
        mapper.deleteByIds(ids);
    }

    @Override
    public Position getById(Long id) {
        PositionDO postDO = mapper.selectById(id);
        if(postDO == null){
            return null;
        }
        return convertor.toDomainObject(postDO);
    }

    @Override
    public Position getByCodeAndCompanyId(String code, Long companyId) {
        PositionDO postDO = mapper.selectByCodeAndCompanyId(code, companyId);
        if(postDO == null){
            return null;
        }
        return convertor.toDomainObject(postDO);
    }

    @Override
    public List<Position> getByIds(Set<Long> postIds) {
        LambdaQueryWrapper<PositionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PositionDO::getId, postIds);
        List<PositionDO> postDOS = mapper.selectList(wrapper);
        if(CollUtil.isEmpty(postDOS)){
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(postDOS);
    }

    @Override
    public List<Position> listByCondition(PositionCriteria criteria) {
        LambdaQueryWrapper<PositionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(!CollUtil.isEmpty(criteria.getPostIds()), PositionDO::getId, criteria.getPostIds());

        wrapper.eq(!StrUtil.isEmpty(criteria.getPostCode()), PositionDO::getCode, criteria.getPostCode());
        wrapper.like(!StrUtil.isEmpty(criteria.getPostName()), PositionDO::getName, criteria.getPostName());
        wrapper.eq(criteria.getCompanyId() != null, PositionDO::getCompanyId, criteria.getCompanyId());
        List<PositionDO> postDOS = mapper.selectList(wrapper);
        if(CollUtil.isEmpty(postDOS)){
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(postDOS);
    }

    @Override
    public PageResponse<Position> pageByCondition(PositionCriteria criteria) {
        // 创建查询条件
        LambdaQueryWrapper<PositionDO> wrapper = new LambdaQueryWrapper<>();

        LocalDateTime[] createTime = criteria.getCreateTime();
        if (createTime != null) {
            if (createTime.length == 1 && createTime[0] != null) {
                wrapper.ge(PositionDO::getCreateTime, createTime[0]);
            } else if (createTime.length > 1 && createTime[0] != null && createTime[1] != null) {
                wrapper.between(PositionDO::getCreateTime, createTime[0], createTime[1]);
            }
        }

        wrapper.eq(!StrUtil.isEmpty(criteria.getPostCode()), PositionDO::getCode, criteria.getPostCode());
        wrapper.like(!StrUtil.isEmpty(criteria.getPostName()), PositionDO::getName, criteria.getPostName());
        wrapper.eq(criteria.getCompanyId() != null, PositionDO::getCompanyId, criteria.getCompanyId());

        // 创建分页对象，确保使用 postDO
        Page<PositionDO> page = new Page<>(criteria.getPageIndex(), criteria.getPageSize());

        // 排序字段
        if (!StrUtil.isEmpty(criteria.getOrderBy())) {
            if (PageQuery.DESC.equals(criteria.getOrderDirection())) {
                page.addOrder(OrderItem.desc(criteria.getOrderBy()));
            } else {
                page.addOrder(OrderItem.asc(criteria.getOrderBy()));
            }
        }
        mapper.selectPage(page, wrapper);
        List<Position> posts = convertor.toDomainObjects(page.getRecords());
        return PageResponse.of(posts, (int)page.getTotal(), (int)page.getSize(), (int)page.getCurrent());
    }

} 